<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>预约管理</title>
    <!-- 引入Vue 2 -->
    <script src="./vue/vue.js"></script>
    <!-- 引入Element UI -->
    <link rel="stylesheet" href="./element-ui/index.css">
    <script src="./element-ui/index.js"></script>
    <!-- 引入Axios -->
    <script src="./axios/axios.min.js"></script>
    <style>
        :root {
            --primary-color: #2c3e50;
            --secondary-color: #3498db;
            --health-color: #e74c3c;
            --normal-color: #2ecc71;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', '微软雅黑', sans-serif;
        }

        body {
            background: #f5f6fa;
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 20px;
        }

        .health-header {
            text-align: center;
            padding: 1.5rem;
            background: var(--primary-color);
            color: white;
            border-radius: 8px;
            margin-bottom: 2rem;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }

        .appointment-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 1.5rem;
        }

        .appointment-card {
            background: white;
            border-radius: 8px;
            padding: 1rem;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
            position: relative;
            transition: transform 0.3s;
        }

        .appointment-card:hover {
            transform: translateY(-3px);
        }

        .appointment-header {
            display: flex;
            align-items: flex-start;
            margin-bottom: 0.5rem;
        }

        .appointment-avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: var(--secondary-color);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1rem;
            margin-right: 1rem;
        }

        .appointment-info {
            flex: 1;
        }

        .appointment-name {
            font-size: 1.2rem;
            color: var(--primary-color);
            margin-bottom: 0.3rem;
        }

        .appointment-status {
            display: flex;
            flex-wrap: wrap;
            gap: 0.5rem;
            margin-bottom: 0.5rem;
        }

        .status-item {
            flex: 1;
            min-width: 100px;
            text-align: left;
            padding: 0.5rem;
            border-radius: 4px;
            background: #f8f9fa;
        }

        .status-label {
            font-size: 0.8rem;
            color: #7f8c8d;
            margin-bottom: 0.3rem;
        }

        .status-value {
            font-size: 1rem;
            font-weight: 600;
            color: var(--primary-color);
        }

        .appointment-details {
            border-top: 1px solid #eee;
            padding-top: 0.5rem;
        }

        .badge {
            padding: 0.2rem 0.6rem;
            border-radius: 16px;
            font-size: 0.75rem;
            display: inline-block;
        }

        .pending {
            background: rgba(255, 224, 224, 0.3);
            color: var(--primary-color);
        }

        .accepted {
            background: rgba(255, 255, 204, 0.3);
            color: var(--primary-color);
        }

        .completed {
            background: rgba(204, 255, 204, 0.3);
            color: var(--primary-color);
        }

        .error-message {
            padding: 15px;
            background: #ffebee;
            border-radius: 8px;
            text-align: center;
            color: #c62828;
        }

        .retry-btn {
            margin-top: 10px;
            padding: 5px 12px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div id="app" class="container">
    <div class="health-header">
        <h1>预约管理</h1>
        <p id="update-time">数据加载中...</p>
    </div>

    <div class="appointment-grid" id="appointmentRecords">
        <div v-for="appointment in appointmentData" :key="appointment.id" :class="['appointment-card', getStateClass(appointment.state)]">
            <div class="appointment-header">
                <div class="appointment-avatar">{{ appointment.name.charAt(0) }}</div>
                <div class="appointment-info">
                    <h2 class="appointment-name">{{ appointment.name }}</h2>
                    <div class="appointment-status">
                        <div class="status-item">
                            <div class="status-label">性别</div>
                            <div class="status-value">{{ appointment.gender === '0' ? '男' : '女' }}</div>
                        </div>
                        <div class="status-item">
                            <div class="status-label">预约项目</div>
                            <div class="status-value">{{ appointment.item }}</div>
                        </div>
                        <div class="status-item">
                            <div class="status-label">处理医生</div>
                            <div class="status-value">{{ appointment.doctorName }}</div>
                        </div>
                        <div class="status-item">
                            <div class="status-label">预约时间</div>
                            <div class="status-value">{{ formatDate(appointment.time) }}</div>
                        </div>
                        <div class="status-item">
                            <div class="status-label">状态</div>
                            <div class="status-value">{{ getStateLabel(appointment.state) }}</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="appointment-details">
                <div class="status-item">
                    <div class="status-label">预约描述</div>
                    <div class="status-value">{{ appointment.subscribe || '无描述' }}</div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            appointmentData: []
        },
        mounted() {
            this.getAppointments();
        },
        methods: {
            getAppointments() {
                const token = this.getCookie('token');
                if (!token) {
                    console.error('Token not found in cookies');
                    return;
                }
                axios.get('http://localhost:8000/user/appointment/get', {
                    headers: {
                        'Authorization': `${token}`
                    }
                })
                    .then(response => {
                        if (response.data.code === 200) {
                            this.appointmentData = response.data.data;
                            this.updateTime();
                        } else {
                            this.showError(`请求失败：${response.data.message || '未知错误'}`);
                        }
                    })
                    .catch(err => {
                        document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
                        window.location.href = 'http://localhost:5173';
                        console.error('获取预约数据失败:', err);
                        this.showError('请求失败：网络错误');
                    });
            },
            formatDate(dateString) {
                if (dateString) {
                    const date = new Date(dateString);
                    return date.toLocaleString(); // 或者使用其他格式化方式
                }
                return '';
            },
            getStateLabel(state) {
                switch (state) {
                    case 0:
                        return '已请求';
                    case 1:
                        return '已接受';
                    case 2:
                        return '已完成';
                    default:
                        return '未知状态';
                }
            },
            getStateClass(state) {
                switch (state) {
                    case 0:
                        return 'pending';
                    case 1:
                        return 'accepted';
                    case 2:
                        return 'completed';
                    default:
                        return '';
                }
            },
            getCookie(name) {
                const value = `; ${document.cookie}`;
                const parts = value.split(`; ${name}=`);
                if (parts.length === 2) return parts.pop().split(';').shift();
            },
            updateTime() {
                document.getElementById('update-time').textContent = `最后更新：${new Date().toLocaleString()}`;
            },
            showError(message) {
                const container = document.getElementById('appointmentRecords');
                container.innerHTML = `
                    <div class="error-message">
                        ⚠️ ${message}
                        <button class="retry-btn">重新加载</button>
                    </div>
                `;
                container.querySelector('.retry-btn').addEventListener('click', this.getAppointments);
            }
        }
    });
</script>
</body>
</html>
